﻿using System.Windows;
using Red.Controls.TimeLine.Core;
using Red.Controls.TimeLine.ViewModels;

namespace Red.Controls.TimeLine.TracksTree
{
    public class TrackPanel : TimePanel
    {     
        public TrackPanel()
        {
        }

        protected override Size ArrangeOverride( Size arrangeSize )
        {
            // var sortedChildren = InternalChildren.Cast<UIElement>().OrderBy<UIElement, double>( new Func<UIElement, double>( delegate ( UIElement child ) { return GetTime( child ); } ) );

            foreach ( UIElement child in InternalChildren )
            {
                FrameworkElement element = (FrameworkElement)child;
                if ( element.DataContext is TrackItemBase )
                {
                    TrackItemBase trackObject = (TrackItemBase)element.DataContext;

                    TimeLineControler controler = trackObject.TimeLineControler;

                    double x = controler.TimeToScreenOffset( trackObject.ActionTime );
                    double width = child.DesiredSize.Width;

                    child.Arrange( new Rect( new Point( x, 0 ), new Size( width, child.DesiredSize.Height ) ) );
                }
            }
            return arrangeSize;
        }  
    }
}